/*
 * Copyright (C) 2012  Forklabs Daniel Léonard
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package ca.forklabs.wow.net;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.GeneralSecurityException;

/**
 * Class {@code AuthenticatedBNetConnection} provides secure and authenticated
 * connections.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.wow.net.AuthenticatedBNetConnection">Daniel Léonard</a>
 * @version $Revision$
 */
public class AuthenticatedBNetConnection extends BNetConnection {

//---------------------------
// Instance variables
//---------------------------

   /** The public key. */
   private String public_key;
   /** The private key. */
   private String private_key;


 //---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   public_key   the public key.
    * @param   private_key   the private key.
    */
   public AuthenticatedBNetConnection(String public_key, String private_key) {
      this.setPublicKey(public_key);
      this.setPrivateKey(private_key);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gets the public key.
    * @return   the public key
    */
   protected String getPublicKey() {
      return this.public_key;
      }

   /**
    * Changes the public key.
    * @param   key   the new public key.
    */
   protected void setPublicKey(String key) {
      this.public_key = key;
      }

   /**
    * Gets the private key.
    * @return   the private key
    */
   protected String getPrivateKey() {
      return this.private_key;
      }

   /**
    * Changes the private key.
    * @param   key   the new private key.
    */
   protected void setPrivateKey(String key) {
      this.private_key = key;
      }


//---------------------------
// Overridden methods from ca.forklabs.wow.net.BNetConnection
//---------------------------

   /**
    * Now opens a secure and authenticated connection.
    */
   @Override
   public HttpURLConnection openConnection(Region region, String resource) throws IOException {
      HttpURLConnection connection = this.openSecureConnection(region, resource);
      return connection;
      }

   /**
    * Now opens a secure and authenticated connection.
    */
   @Override
   public HttpURLConnection openConnection(Region region, String resource, long last_modified) throws IOException {
      HttpURLConnection connection = this.openSecureConnection(region, resource, last_modified);
      return connection;
      }

   /**
    * Now opens a secure and authenticated connection.
    */
   @Override
   @SuppressWarnings("hiding")
   public HttpURLConnection openSecureConnection(Region region, String resource, long last_modified) throws IOException {
      String public_key = this.getPublicKey();
      String private_key = this.getPrivateKey();

   // one needs to hide the general security exception
   // because the throws clause cannot be modified
      HttpURLConnection connection;
      try {
         connection = this.openConnection(region, resource, last_modified, public_key, private_key);
         }
      catch (GeneralSecurityException gse) {
         throw new IOException(gse);
         }
      return connection;
      }

   }
